package com.burgeon.backend.SHOP.rest;
/**
 * 获取店铺
 */


import com.burgeon.backend.Rest.base.RESTBase;

import com.burgeon.backend.SHOP.entity.Store;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindStoreData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindStoreData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     */

    public ArrayList<Store> getData() throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(), url);

        String count = new JSONArray(str).getJSONObject(0).getString("count");


        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Store> data = new ArrayList<>();
        for (int i =0;i<jsonArray.length();i++){
            JSONArray jsonArray1 = jsonArray.getJSONArray(i);
            Store store = new Store();
            store.setId(jsonArray1.getString(0));
            store.setName(jsonArray1.getString(1));
            store.setLongitude(jsonArray1.getDouble(2));
            store.setLatitude(jsonArray1.getDouble(3));
            store.setMap(jsonArray1.getString(4).replace("null","-"));
            store.setAddress(jsonArray1.getString(5).replace("null","-"));
            data.add(store);
        }


        return data;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        columns.add("ID");
        columns.add("NAME");//店
        columns.add("LONGITUDE");//经度
        columns.add("LATITUDE");//纬度
        columns.add("MAP");//地图
        columns.add("ADDRESS");//地址

        params.put("columns", columns);
        params.put("table", "C_STORE");//表名

        params.put("count", "true");

        HashMap orderby = new HashMap();

        orderby.put("column", "CREATIONDATE");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        HashMap expr2 = new HashMap();

        HashMap expr1v1 = new HashMap();
        HashMap expr1v2 = new HashMap();


        expr1.put("combine", "and");

        expr1v1.put("column", "'LONGITUDE'");//经度
        expr1v1.put("condition", "is not null");
        expr1.put("expr1", expr1v1);

        expr1v2.put("column", "'LATITUDE'");//纬度
        expr1v2.put("condition", "is not null");
        expr1.put("expr2", expr1v2);

        HashMap expr2v1 = new HashMap();
        HashMap expr2v2 = new HashMap();

        expr2.put("combine", "and");

        expr2v1.put("column", "'ISACTIVE'");//
        expr2v1.put("condition", "Y");
        expr2.put("expr1", expr2v1);

        expr2v2.put("column", "'IS_YC'");//
        expr2v2.put("condition", "Y");
        expr2.put("expr2", expr2v2);

        paramss.put("combine", "and");//关系

        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);


        params.put("params", paramss);
        return DATA("Query", params, account, password);
    }


    public static void main(String[] args) {
        FindStoreData g = new FindStoreData("39.99.239.133:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
